package app.server.s03;

import pub.itf.IPubParam;
import pub.utils.TreeUtils;
import app.model.AppBasedoc;
import app.model.AppMenu;
import app.model.AppMessage;

import com.agile.jfinal.controller.CoreController;
import com.jfinal.ext.render.DwzRender;

/**
 * 数据类型参数设置
 * @author qiutian  2014年3月26日
 */
public class AppMenuController extends CoreController {

	/**
	 * 默认方法查询数据
	 */
	public void index(){
		//生成全部数据的树形菜单
		try {
			
			//获取查询条件
			AppBasedoc appBasedoc = getModel(AppBasedoc.class);
			setAttr("appBasedoc", appBasedoc.queryById());
			
			//查询树形菜单
			String pk_basedoc = appBasedoc.getPrimaryValue();
			setAttr("tree",TreeUtils.createTreeHtml(getModel(AppMenu.class).set(AppMenu.PK_BASEDOC, pk_basedoc).query(" order by vcode "),getRequest().getContextPath()+"/appMenu/viewPage"));
			
			renderJsp("/WEB-INF/jsp/app/server/s03/app_menu_list.jsp");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			render(DwzRender.error(e.getMessage()));
		}
	}

	/**
	 * 保存数据
	 */
	public void saveModel(){

		try {
			
			AppMenu AppMenu = getModel(AppMenu.class);
			
			//获取参照数据
			AppBasedoc appBasedoc = getModel(AppBasedoc.class); 
			AppMenu.set(AppMenu.PK_BASEDOC, appBasedoc.getPrimaryValue());
			
			if(AppMenu.get(AppMenu.getPrimaryKey())==null){   //新增保存 
				AppMenu.addAppMenuModel();
			}else{ //修改
				AppMenu.save();
			}
			DwzRender dwz = new DwzRender();
			dwz.message("操作成功");
			dwz.callbackType("closeCurrent");
			dwz.navTabId("app_menu_list");
			dwz.forwardUrl("/appMenu/index?appBasedoc.pk_basedoc="+appBasedoc.getPrimaryValue());
			render(dwz);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			render(DwzRender.error());
		}
	}
	
	/**
	 * 转入新增页面
	 */
	public void addPage(){
		//获取参照数据   并在页面加载
		AppBasedoc appBasedoc = getModel(AppBasedoc.class); 
		setAttr("appBasedoc", appBasedoc.queryById());
		
		//获取选中数据生成父主键
		AppMenu appMenu = getModel(AppMenu.class); //获取选中数据
		String pk_menu = appMenu.getPrimaryValue();
		
		if(pk_menu.equals("")){
			pk_menu = IPubParam.pk_parent_dafultValue;
		}
		
		appMenu.set(appMenu.PK_PARENT, pk_menu); //将选中数据的主键作为新数据的父主键

		appMenu.set(appMenu.getPrimaryKey(), null);
		setAttr("appMenu", appMenu);
		renderJsp("/WEB-INF/jsp/app/server/s03/app_menu_form.jsp");
	}
	
	/**
	 * 转入修改页面
	 */
	public void editPage(){
		
		//获取参照数据   并在页面加载
		AppBasedoc appBasedoc = getModel(AppBasedoc.class); 
		setAttr("appBasedoc", appBasedoc.queryById());
				
		setAttr("appMenu", getModel(AppMenu.class).queryById());
		renderJsp("/WEB-INF/jsp/app/server/s03/app_menu_form.jsp");
		
	}
	
	/**
	 * 转入查看页面
	 */
	public void viewPage(){
		
		AppMenu appMenu = new AppMenu().set(AppMenu.PK_MENU, getPara(AppMenu.PK_MENU)).queryById();
		
		//获取参照数据并在页面加载
		AppBasedoc appBasedoc = getModel(AppBasedoc.class); 
		setAttr("appBasedoc", appBasedoc.setPrimaryValue(appMenu.getStr(appBasedoc.PK_BASEDOC)).queryById());
		setAttr("appMenu", appMenu);
		
		renderJsp("/WEB-INF/jsp/app/server/s03/app_menu_view.jsp");
		
	}
	
	/**
	 * 删除数据
	 */
	public void delModel(){
		//获取参照数据
		AppBasedoc appBasedoc = getModel(AppBasedoc.class); 
		
		AppMenu appMenu = getModel(AppMenu.class);
		
		String pk_basedoc = appMenu.getStr(appMenu.PK_BASEDOC);
		
		appMenu.delAppMenuModel();
		DwzRender dwz = new DwzRender();
		dwz.message("操作成功");
		dwz.navTabId("app_menu_list");
		dwz.forwardUrl("/appMenu/index?appBasedoc.pk_basedoc="+pk_basedoc);
		render(dwz);
	}
}
